<?php

namespace App\Shop\Controllers;

use App\Admin\Extensions\Table\ServiceSelectTable;
use App\Admin\Extensions\Table\ShopSelectTable;
use App\Admin\Repositories\Coupon;
use App\Models\Area;
use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\BusinessCategory;
use App\Models\ShopUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CouponController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Coupon(), function (Grid $grid) {
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("shop_id",$shop_id);
            $grid->column('id')->sortable();
//            $grid->column('mode');
            $grid->column('title');
            $grid->column('range')->using(\App\Models\Coupon::getRanges());
            $grid->column('full_price');
            $grid->column('price');
            $grid->column('days');
            $grid->column('limit_time','到期时间');
            $grid->column('cid');
            $grid->column('service_id');
            $grid->column('status')->using(['正常','隐藏']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('title');
            });
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Coupon(), function (Form $form) {
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $form->hidden('shop_id')->default($shop_id)->value($shop_id);
            $form->hidden('mode')->default(0); //洗车券
            $form->text('title')->required();
            $form->currency('full_price')->symbol("￥")->required();
            $form->currency('price')->symbol("￥")->required();
            $form->number('days')->default(1)->min(1)->required();
            $form->date('limit_time','到期时间')->help("到期时间一旦过期,用户就不能领取此优惠券")->required();
            $form->radio('range')
                ->options(\App\Models\Coupon::getRanges())
                ->default(0)->when(1,function (Form $form){
                    $form->select('cid')->options(ServiceCategory::getOptions())->width(4);
                })->when(2,function (Form $form){
                    $form->selectTable('service_id','所属服务')->title('选择服务')
                        ->dialogWidth('60%')
                        ->from(ServiceSelectTable::make(['id' => $form->getKey()]))
                        ->model(Service::class)->width(4);
                });
//            $form->category("areas","显示区域")
//                ->checkStrategy()
//                ->multiple()->maxCount()
//                ->options(Area::getOptions())
//                ->help("留空不设置则全国显示与使用;设置地区表示指定地区显示与使用");
            $form->radio('status')->options(['正常','隐藏'])->default(0);

        });
    }
}
